﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace QuickLifeHall.ERP
{
    public partial class login : System.Web.UI.Page
    {
        DAL.SystemBase.User userRepository = new DAL.SystemBase.User();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                try
                {
                    HttpCookie userLoginCookie = Request.Cookies["userLoginCookie"];
                    this.txtUsername.Text = userLoginCookie["loginName"];
                }
                catch { }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string loginName = this.txtUsername.Text.Trim();
            string password = this.txtPassword.Text.Trim();

            Entity.SystemBase.User userEntity = userRepository.GetByName(loginName);

            if (userEntity == null)
            {
                lblMessage.Text = "用户不存在！";
                lblMessage.Visible = true;
                return;
            }

            if (userEntity.UserPassword != password)
            {
                lblMessage.Text = "用户名或密码错误，请重新输入！";
                lblMessage.Visible = true;
                return;
            }

            //保存用户登录名
            HttpCookie userLoginCookie = new HttpCookie("userLoginCookie");
            userLoginCookie["loginName"] = userEntity.UserName;
            userLoginCookie.Expires = DateTime.Now.AddYears(1);
            this.Response.Cookies.Add(userLoginCookie);

            //页面跳转
            FormsAuthentication.RedirectFromLoginPage(userEntity.UserName, false);
        }
    }
}